var ctx=window.daijia.ctx;

$(document).ready(function(){

    var boundx,boundy,x=0,y=0,width=400,height=200;
    $("#photo_upload_dialog").click(function(){
		$("#dialog_local_photo").modal();
	});
	$('#photolocalsnap').click(function(){
	 	$('#photoFile').click();
	});

	$(document).on('change','#photoFile',function(){
		if($.trim($(this).val())){
			$("#photolocalsnap").html("上传中...");
			$("#photolocalsnap").attr("disabled",true);
		 	$.ajaxFileUpload({
                url:ctx+'/common/uploadTemp',
                secureuri:false,
                fileElementId:'photoFile',
                dataType: 'json',
                data:{'type':'driverCarTemp'},
                success: function (result, textStatus) {
                	if(result.success){
                		$("#photolocalsnap").html("本地上传");
            			$("#photolocalsnap").attr("disabled",false);
            			
            			$("#photoImg_div").html('<img id="photoImg" style="max-width:400px;max-height:400px;" src="'+ctx+'/upload/driverCarTemp/'+result.url+'"/>');
            			
            			$("#photoVideo").parent().hide();
            			$("#photoImg_div").show();
                    	
                    	$("#photoImg_container").html('<img src="'+ctx+'/upload/driverCarTemp/'+result.url+'"  />');
                    	
                    	$("#photoImg").Jcrop({
                    		'bgFade':true,
                    		'keySupport':false,
                    		'aspectRatio':2,
                    		'onChange': updatePhotoPreview,
                    		'onSelect': updatePhotoPreview
                    	},function(){
                    		var bounds = this.getBounds();
                    		boundx = bounds[0];
                    		boundy = bounds[1];
                    		this.setSelect([0,0,400,200]);
                    	});
                    	
                    	
                	}
                	else{
                		$.scojs_message(result.msg, $.scojs_message.TYPE_ERROR);
                		$("#photolocalsnap").html("本地上传");
            			$("#photolocalsnap").attr("disabled",false);
                	}
                }
            });
		}
	});
	function updatePhotoPreview(c){
		if (parseInt(c.w) > 0) {
			var rx = 400 / c.w;
			var ry = 200 / c.h;
			x=c.x;
			y=c.y;
			width=c.w;
			height=c.h;
			
			$("#photoImg_container img").css({
				width: Math.round(rx * boundx) + 'px',
				height: Math.round(ry * boundy) + 'px',
				marginLeft: '-' + Math.round(rx * c.x) + 'px',
				marginTop: '-' + Math.round(ry * c.y) + 'px'
			});
		}
	};
	
	$("#photo_ok").click(function(){
		if($("#photoImg").attr("src")){
			$("#photo_ok").attr("disabled",true);
			$.post(ctx+'/common/photoScale',{'type':'driverCar','url':$("#photoImg").attr("src"),'x':x,'y':y,'width':parseInt(width),'height':parseInt(height)},function(result){
				result=$.trim(result);
				$("#show_photo").attr("src",ctx+"/upload/driverCar/"+result);
	        	$("#picture").attr('value',result)
	        	$("#photo_ok").attr("disabled",false);
	        	$("#photoImg_div").html('');
	        	$("#photoImg_container").html('');
	        	$("#dialog_local_photo").modal("hide");
	        });
		}
		else{
			alert("请上传服务人员照片");
		}
	});
	


   $("#photo_upload_zhuche_car").click(function(){
			 $("#zhuche_img_up").click();
   });
	$(document).on("change","#zhuche_img_up",function(){
		$.ajaxFileUpload({
			url:ctx+'/common/uploadZhucheCarImage',
			secureuri:false,
			fileElementId:'zhuche_img_up',
			dataType: 'json',
			success: function (result, textStatus) {
				if(result.success){
				//	$("#photoImg_div").html('<img id="photoImg" style="max-width:400px;max-height:400px;" src="'+ctx+'/upload/driverCarTemp/'+result.url+'"/>');
                    $("#show_photo").attr("src",result.url);
					$("#picture").val(result.url);
				//	$("#photoImg_container").html('<img src="'+ctx+'/upload/driverCarTemp/'+result.url+'"  />');
				}
				else{
					$.scojs_message(result.msg, $.scojs_message.TYPE_ERROR);
				}
			}
		});
	});







	
});

